/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.StWorkflow;
import com.thinkgem.jeesite.modules.ta.service.StWorkflowService;
import com.thinkgem.jeesite.modules.ta.util.WebServiceUtil;

/**
 * 事务申请Controller
 * @author 刘雨轩
 * @version 2017-05-26
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stWorkflow")
public class StWorkflowController extends BaseController {

	@Autowired
	private StWorkflowService stWorkflowService;
	
	@ModelAttribute
	public StWorkflow get(@RequestParam(required=false) String id) {
		StWorkflow entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = stWorkflowService.get(id);
		}
		if (entity == null){
			entity = new StWorkflow();
		}
		return entity;
	}
	
	@RequiresPermissions("ta:stWorkflow:view")
	@RequestMapping(value = {"list", ""})
	public String list(StWorkflow stWorkflow, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StWorkflow> page = stWorkflowService.findPage(new Page<StWorkflow>(request, response), stWorkflow); 
		model.addAttribute("page", page);
		return "modules/ta/stWorkflowList";
	}

	@RequiresPermissions("ta:stWorkflow:view")
	@RequestMapping(value = "form")
	public String form(StWorkflow stWorkflow, Model model) {
		model.addAttribute("stWorkflow", stWorkflow);
		return "modules/ta/stWorkflowForm";
	}

	@RequiresPermissions("ta:stWorkflow:edit")
	@RequestMapping(value = "save")
	public String save(StWorkflow stWorkflow, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stWorkflow)){
			return form(stWorkflow, model);
		}
		stWorkflowService.save(stWorkflow);
		addMessage(redirectAttributes, "保存事务成功");
		return "redirect:"+Global.getAdminPath()+"/ta/stWorkflow/?repage";
	}
	
	@RequiresPermissions("ta:stWorkflow:edit")
	@RequestMapping(value = "delete")
	public String delete(StWorkflow stWorkflow, RedirectAttributes redirectAttributes) {
		stWorkflowService.delete(stWorkflow);
		addMessage(redirectAttributes, "删除事务成功");
		return "redirect:"+Global.getAdminPath()+"/ta/stWorkflow/?repage";
	}
	
	@RequiresPermissions("ta:stWorkflow:edit")
	@RequestMapping(value = "inv")
	public String invokeRemoteFuc(String wid,HttpServletRequest request){
		User user = UserUtils.getUser();
		if(user == null){
			return "modules/sys/sysLogin";
		}
		String userId = user.getNo();
		String ownerName = user.getName();
		WebServiceUtil.invokeRemoteFuc(wid, userId, ownerName,request);
		return "redirect:"+Global.getAdminPath()+"/ta/stWorkflow/?repage";
	}
}